<?php

namespace Common\Model;

use Think\Model;

class LinkModel extends Model
{
    protected $_validate = array(
        array('link_name', 'require', '名称必须填写！', 1),
        array('link_url', 'url', '请正确填写链接地址！', 1),
    );

    protected $_auto = array(
        array('addtime', 'time', 1, 'function'),
    );

    /**
     * 获取列表
     *
     * @param        $map
     * @param bool   $page
     * @param string $sort
     * @param string $num
     * @return mixed
     */
    public function get_list($map, $page = false, $sort = '', $num = '')
    {
        $limit = $num ? $num : 10;
        if (empty($sort)) {
            $sort = 'sort DESC,id DESC';
        }
        if ($page) {
            $count    = $this->where($map)->count();
            $Page     = pager($count, $limit);
            $limit    = $Page->firstRow . ',' . $Page->listRows;
            $page_str = $Page->fshow();
        }
        $result = $this->where($map)->order($sort)->limit($limit)->select();
        foreach ($result as $key => $v) {

            $v['url'] = RU('intro/view', array('id' => $v['id']));
            $list[]   = $v;
        }
        $return['list'] = $list;
        if ($page) {
            $return['count'] = $count;
        } else {
            $return['count'] = count($list);
        }
        $return['page'] = $page_str;

        return $return;
    }

}